package com.example.database;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.model.OutProfile;
import com.example.util.Util;

public class DBManager {
	// Database fields
	private SQLiteDatabase database;
	private MySQLiteHelper dbHelper;
	
	private String[] allColumns = { 
			
			MySQLiteHelper.COLUMN_ID,                // 0
			MySQLiteHelper.COLUMN_EMAIL,             // 1
			
			MySQLiteHelper.COLUMN_FIRST_NAME,        // 2 
			MySQLiteHelper.COLUMN_LAST_NAME,         // 3
			MySQLiteHelper.COLUMN_GENDER,            // 4
			MySQLiteHelper.COLUMN_BIRTHDAY,          // 5
			MySQLiteHelper.COLUMN_MOBILE,            // 6
			MySQLiteHelper.COLUMN_HOME_PHONE,        // 7
			MySQLiteHelper.COLUMN_HOME_ADDRESS,      // 8
			
			MySQLiteHelper.COLUMN_COMPANY_NAME,      // 9
			MySQLiteHelper.COLUMN_COMPANY_WEBSITE,   // 10
			MySQLiteHelper.COLUMN_COMPANY_EMAIL,     // 12
			MySQLiteHelper.COLUMN_COMPANY_PHONE,     // 11
			MySQLiteHelper.COLUMN_COMPANY_ADDRESS,   // 13
			 };

	public DBManager(Context context) {
		dbHelper = new MySQLiteHelper(context);
	}

	public void open() throws SQLException {
		database = dbHelper.getWritableDatabase();
	}
	

	public void close() {
		dbHelper.close();
	}
	public long createProfile(OutProfile profile) {
		try {
			ContentValues cv = new ContentValues();
			
			cv.put(MySQLiteHelper.COLUMN_ID, profile.getId());
			cv.put(MySQLiteHelper.COLUMN_EMAIL, profile.get_email());
			cv.put(MySQLiteHelper.COLUMN_FIRST_NAME, profile.get_first_name());
			cv.put(MySQLiteHelper.COLUMN_LAST_NAME, profile.get_last_name());
			cv.put(MySQLiteHelper.COLUMN_GENDER,profile.get_gender());
			cv.put(MySQLiteHelper.COLUMN_BIRTHDAY, profile.get_birthday());
			cv.put(MySQLiteHelper.COLUMN_MOBILE, profile.get_mobile());
			cv.put(MySQLiteHelper.COLUMN_HOME_PHONE, profile.get_home_phone());
			cv.put(MySQLiteHelper.COLUMN_HOME_ADDRESS, profile.get_home_address());
			
			cv.put(MySQLiteHelper.COLUMN_COMPANY_NAME, profile.get_company_name());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_WEBSITE, profile.get_company_website());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_PHONE, profile.get_company_phone());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_EMAIL, profile.get_company_email());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_ADDRESS, profile.get_company_address());
			
			return database.insert(MySQLiteHelper.TABLE_PROFILE, null, cv);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return -1;
	}

	// public OutProfile createProfile(OutProfile profile) {
	// ContentValues values = new ContentValues();
	// values.put(MySQLiteHelper.COLUMN_EMAIL, profile.getEmail());
	// long insertId = database.insert(MySQLiteHelper.TABLE_PROFILE, null,
	// values);
	// Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
	// allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
	// null, null, null);
	// cursor.moveToFirst();
	// OutProfile bProfile = cursorToProfile(cursor);
	// cursor.close();
	// return bProfile;
	// }

	public boolean checkEmail(int id) {
		try {
			Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
					new String[] { MySQLiteHelper.COLUMN_ID },
					MySQLiteHelper.COLUMN_ID + " = " + id , null,
					null, null, null);
			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				final int tempEmail = cursor.getInt(0);
				cursor.close();
				if ( id != -1 && tempEmail== id)return true;
				return false;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public void deleteProfile(OutProfile profile) {
		long id = profile.getId();
		System.out.println("Comment deleted with id: " + id);
		database.delete(MySQLiteHelper.TABLE_PROFILE, MySQLiteHelper.COLUMN_ID
				+ " = " + id, null);
	}

	public List<OutProfile> getAllProfile() {
		List<OutProfile> profiles = new ArrayList<OutProfile>();

		Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
				allColumns, null, null, null, null, null);

		cursor.moveToFirst();
		while (!cursor.isAfterLast()) {
			OutProfile profile = cursorToProfile(cursor);
			profiles.add(profile);
			cursor.moveToNext();
		}
		// Make sure to close the cursor
		cursor.close();
		return profiles;
	}

	public OutProfile getProfile(String id) {
		OutProfile profile = null;
		try {
			Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
					allColumns, MySQLiteHelper.COLUMN_ID + " = " + id, null, null, null, null);
           if(cursor != null && cursor.getCount()>0){
        	   cursor.moveToFirst();
   			   profile = cursorToProfile(cursor);
   			   // Make sure to close the cursor
           }
			
			cursor.close();
		} catch (Exception ex) {
			
			ex.printStackTrace();
		}
		
		return profile;
	}
	public OutProfile getProfile(int id) {
		OutProfile profile = null;
		try {
			Cursor cursor = database.query(MySQLiteHelper.TABLE_PROFILE,
					allColumns, MySQLiteHelper.COLUMN_ID + " = " + id
							, null, null, null, null);
           if(cursor != null && cursor.getCount()>0){
        	   cursor.moveToFirst();
   			   profile = cursorToProfile(cursor);
   			   // Make sure to close the cursor
           }
			
			cursor.close();
		} catch (Exception ex) {
			
			ex.printStackTrace();
		}
		
		return profile;
	}


	private OutProfile cursorToProfile(Cursor cursor) {
    
		OutProfile profile = new OutProfile();
		profile.setId(cursor.getLong(0) + "");
		profile.set_email(cursor.getString(1));
		
		profile.set_first_name(cursor.getString(2));
		profile.set_last_name(cursor.getString(3));
		profile.set_gender(cursor.getInt(4));
		profile.set_birthday(cursor.getString(5));
		profile.set_mobile(cursor.getString(6));
		profile.set_home_phone(cursor.getString(7));
		profile.set_home_address(cursor.getString(8));
		
		profile.set_company_name(cursor.getString(9));
		profile.set_company_website(cursor.getString(10));
		profile.set_company_email(cursor.getString(11));
		profile.set_company_phone(cursor.getString(12));
		profile.set_company_address(cursor.getString(13));
		return profile;
	}
    public int deleteProfile(String id){
    	return database.delete(MySQLiteHelper.TABLE_PROFILE, "_id=?", new String[]{ id });              
    }
	public boolean updateProfile(OutProfile profile) {
		try {
			ContentValues cv = new ContentValues();
			//cv.put(MySQLiteHelper.COLUMN_ID, profile.getId());
			cv.put(MySQLiteHelper.COLUMN_EMAIL, profile.get_email());
			cv.put(MySQLiteHelper.COLUMN_FIRST_NAME, profile.get_first_name());
			cv.put(MySQLiteHelper.COLUMN_LAST_NAME, profile.get_last_name());
			cv.put(MySQLiteHelper.COLUMN_GENDER,profile.get_gender());
			cv.put(MySQLiteHelper.COLUMN_BIRTHDAY, profile.get_birthday());
			cv.put(MySQLiteHelper.COLUMN_MOBILE, profile.get_mobile());
			cv.put(MySQLiteHelper.COLUMN_HOME_PHONE, profile.get_home_phone());
			cv.put(MySQLiteHelper.COLUMN_HOME_ADDRESS, profile.get_home_address());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_NAME, profile.get_company_name());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_WEBSITE, profile.get_company_website());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_EMAIL, profile.get_company_email());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_PHONE, profile.get_company_phone());
			cv.put(MySQLiteHelper.COLUMN_COMPANY_ADDRESS, profile.get_company_address());
			return this.database.update(MySQLiteHelper.TABLE_PROFILE, cv,
					MySQLiteHelper.COLUMN_ID + " = " + profile.getId() , null) > 0;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return false;
	}
}
